<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <form class="layui-form">
                <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">作业单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="code" class="layui-input" type="text" placeholder="作业单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">计划单编号：</label>
                                    <div class="layui-input-inline">
                                        <input name="planCode" class="layui-input" type="text" placeholder="计划单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">发货仓：</label>
                                    <div class="layui-input-inline">
                                        <select name="outWarehouseId">
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input  id="pTime" class="layui-input date-icon" type="text"
                                                placeholder="选择创建时间" autocomplete="off" readonly/>
                                    </div>
                                    <input name="startCreateTime" id="sTime" style="display: none"/>
                                    <input name="endCreateTime" id="eTime" style="display: none"/>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button type="button" id="inventoryInoutSearch" class="layui-btn icon-btn color-green" lay-filter="inventoryOutPlanSearch" lay-submit>查询</button>
                                    <button type="reset" class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            </form>
            <!-- 表格 -->
            <table class="layui-table" id="inventoryOutPlanTable" lay-filter="inventoryOutPlanTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="inventoryOutPlanTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details">详情</a>
</script>
<!-- 入库作业记录详情操作栏 -->
<script type="text/html" id="outWorkDetailsTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="particulars">明细</a>
</script>
<!-- 入库物料明细详情弹窗 -->
<script type="text/html" id="outProductParticularsTableForm">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-table" id="outProductParticularsTable" lay-filter="outProductParticularsTable"></table>
                <table class="layui-table" id="outProductParticularsTable2" lay-filter="outProductParticularsTable2"></table>
                <div class="layui-form-item" style="text-align: center;margin-top: 20px">
                    <button class="layui-btn" type="button" id="backBtn" lay-filter="inventoryOutPlanFormSubmit" lay-submit>返回</button>
                </div>
            </div>
        </div>
    </div>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="inventoryOutPlanForm">
    <form lay-filter="inventoryOutPlanForm"  class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div id="codeSpan" class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>单据编号</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="code"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>发货商</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="fhs">湖南云端商业管理有限公司</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>所属入库计划</label>
            <div class="layui-form-mid layui-word-aux">
                <span id="planCode" style="color: red" ></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">出货仓库</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="outWarehouseName"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">运输方式</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="transportTypeDesc"></span>
            </div>
        </div>

        <div id="logisticsInfo" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">物流企业</label>
                <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="logistics"></span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">物流单号</label>
                <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="expressCode"></span>
                </div>
            </div>
        </div>

        <div id="customerInfo" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车牌号</label>
                <div class="layui-form-mid layui-word-aux">
                    <span style="color: red" id="carNo"></span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">联系人</label>
                <div class="layui-form-mid layui-word-aux">
                    <span style="color: red" id="customerConcat"></span>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">电话</label>
                <div class="layui-form-mid layui-word-aux">
                    <span style="color: red" id="customerMoble"></span>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>签收仓库</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="intWarehouseName"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;"><span style="color: red">*</span>备注信息</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="planRemark"></span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">入库商品</label>
        </div>
        <table class="layui-table" id="outWorkDetailsTable" lay-filter="outWorkDetailsTable"></table>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">备注描述</label>
            <div class="layui-form-mid layui-word-aux">
                <span style="color: red" id="remark"></span>
            </div>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin','laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var laydate = layui.laydate;

        //日期范围
        laydate.render({
            elem: '#pTime',
            type: 'date',
            range: true,
            done: function(value, date, endDate){
                if(value){
                    let dateValue = value.split(" - ");
                    $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
                    $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
                }else {
                    $("input[name='startCreateTime']").val("");
                    $("input[name='endCreateTime']").val("");
                }
            }
        });
        // option渲染
        function loadSelectHtml(data, tips, htmltarget, type) {
            let html = ' <option value>' + tips + '</option>';
            if (!data || data == null) {
                return;
            }
            for (let i = 0; i < data.length; i++) {
                if (type == 1) {
                    html += `<option value='${data[i].productType}'>${data[i].name}</option>`;
                } else if (type == 2) {
                    html += `<option value='${data[i].id}'>${data[i].name}</option>`;
                } else if (type == 3) {
                    html += `<option value='${data[i].id}'>${data[i].warehouseName}</option>`;
                }else if(type == 4){
                    html += `<option value='${data[i].skuCode}'  skuCode='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if(type == 5){
                    html += `<option value='${data[i].skuCode}'>${data[i].skuName}</option>`;
                } else if(type == 6){
                    html += `<option value='${data[i].code}'>${data[i].name}</option>`;
                }

            }

            $("form select[name='" + htmltarget + "']").html(html);
            form.render('select');
        }
        //查询仓库
        admin.req('enterprisecab/warehouse/admin/warehouse/getWarehouseVOAll', {}, function (res) {
            if (res.code == 200) {
                if (res.data.length > 0){
                    loadSelectHtml(res.data, "选择仓库", "outWarehouseId", 2);
                }
            } else {
                layer.msg(res.msg, {icon: 2});
            }
        }, 'GET');
        // 渲染表格
        var insTb = table.render({
            elem: '#inventoryOutPlanTable',
            url: config.base + 'enterprisecab/order/admin/cloudInWorkRecord/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'}
                ,{field: 'code', sort: true, title: '入库作业单编号', width: 150}
                ,{field: 'planCode',title: '入库计划单编号', width: 150}
                ,{field: 'outWarehouseName',title: '发货仓库'}
                ,{field: 'consigner',title: '发货人'}
                ,{field: 'quantityTotal',title: '入库商品数量', width: 150}
               // ,{field: 'supplyAmount', sort: true, title: '出库商品单价', width: 150}
                ,{field: 'marketAmount', title: '市场总价', width: 160}
                ,{field: 'supplyAmount',title: '总供货金额'}
                ,{field: 'inWarehouseName',title: '入库云仓'}
                //,{field: 'no', sort: true, title: '联系人'}
                ,{field: 'inAreaName', title: '仓库所在地'}
                ,{field: 'statusDesc', title: '状态'}
                ,{field: 'createTime', sort: true, title: '添加时间', width: 160}
                ,{align: 'center', toolbar: '#inventoryOutPlanTableBar', title: '操作', width: 200}
            ]],
            done: function (res, curr, count) {
                //移除按钮
                var menuButton = config.getMenuButton(location.hash);
                if(menuButton.indexOf("搜索") == -1){
                    $('.layui-form.toolbar').remove();
                }
                if(menuButton.indexOf("修改") == -1){
                    $("[lay-event='edit']").remove();
                }
                if(menuButton.indexOf("删除") == -1){
                    $("[lay-event='del']").remove();
                }
            }
        });
        form.render('select');
        // 工具条点击事件
        table.on('tool(inventoryOutPlanTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'details') { //详情
                showEditModel(data);
            } else if (obj.event === 'audit') { // 审核
                doDelete(obj);
            }
        });

        //监听排序
        table.on('sort(inventoryOutPlanTable)', function(obj){
            table.reload('inventoryOutPlanTable', {
                initSort: obj
                ,where: {
                    sort: obj.field
                    ,order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(inventoryOutPlanSearch)', function (data) {
            insTb.reload({where: data.field,page: {curr: 1}},'data');
            return false;
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            console.log("带入的数据",data)
            admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: '云仓入库记录详情',
                content: $('#inventoryOutPlanForm').html(),
                success: function () {
                    $("#code").html(data.code);
                    $("#planCode").html(data.planCode);
                    $("#outWarehouseName").html(data.outWarehouseName);
                    $("#intWarehouseName").html(data.inWarehouseName);
                    admin.req('enterprisecab/order/admin/cloudInWorkRecord/getInventoryJobVOByJobId', {jobId:data.id}, function (res) {
                        if (res.code == 200) {
                            console.log(res.data)
                            $("#transportTypeDesc").html(res.data.transportTypeDesc);
                            $("#planRemark").html(res.data.planRemark);
                            $("#remark").html(res.data.remark);
                            if(res.data.transportType == "logistics" || res.data.transportType  == "express"){
                                $("#logisticsInfo").show();
                                $("#logistics").html(res.data.logistics);
                                $("#expressCode").html(res.data.expressCode);
                            }else{
                                $("#customerInfo").show();
                                $("#carNo").html(res.data.carNo);
                                $("#customerMoble").html(res.data.customerMoble);
                                $("#customerConcat").html(res.data.customerConcat);
                            }

                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    //出货商品详情
                    var outWorkDetailsList = [];
                    admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/getOutWorkProductDetail', {jobId:data.id}, function (res) {
                        if (res.code == 200) {
                            outWorkDetailsList = res.data;
                            table.reload('outWorkDetailsTable',{
                                data: outWorkDetailsList,
                            })
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    var outWorkDetailsTable = table.render({
                        elem: '#outWorkDetailsTable',
                        data: outWorkDetailsList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '平台物料编号', width: 150}
                            , {field: 'productPic', sort: true, title: '图片', width: 120}
                            , {field: 'productName', sort: true, title: '物料名称', width: 150}
                            //, {field: 'skuSpecValueNames', title: 'SKU规格'}
                            , {field: 'skuSpecCode', title: '规格', width: 150}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'total', title: '平台结算价'}
                            , {field: 'quantity', title: '入库数量', width: 120}
                            , {field: 'unitName', title: '单位'}
                            , {field: 'totalMoney', title: '总金额'}
                            , {field: 'planQuantity', title: '待录入数', width: 120,style:'color:red'}
                            , {align: 'center', toolbar: '#outWorkDetailsTableBar', title: '操作', width: 100}
                        ]]
                        ,done: function(res, curr, count){

                        }
                    });
                    // 工具条点击事件
                    table.on('tool(outWorkDetailsTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'particulars') { //明细
                            showOutProductParticularsModel(data);
                        }
                    });
                }
            });
        }
//出库物料明细详情弹窗
        function showOutProductParticularsModel(data) {
            var index = admin.open({
                type: 1,
                area: ['1400px', '800px'],
                offset: '65px',
                title: ' 云仓入库物料信息详情',
                content: $('#outProductParticularsTableForm').html(),
                success: function () {
                    var productParticularsList = [];
                    productParticularsList.push(data);
                    var outProductParticularsTable = table.render({
                        elem: '#outProductParticularsTable',
                        data: productParticularsList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'skuCode', sort: true, title: '物料编号', width: 150}
                            , {field: 'productPic', sort: true, title: '图片', width: 120}
                            , {field: 'productName', sort: true, title: '物料名称', width: 150}
                            //, {field: 'skuSpecValueNames', title: 'SKU规格'}
                            , {field: 'skuSpecCode', title: '规格', width: 150}
                            , {field: 'enterpriceCode', title: '企业货号'}
                            , {field: 'barcode', title: '国标条码'}
                            , {field: 'total', title: '平台结算价'}
                            , {field: 'quantity', title: '入库数量', width: 120}
                            , {field: 'unitName', title: '单位'}
                            , {field: 'totalMoney', title: '总金额'}
                            , {field: 'planQuantity', title: '待录入数', width: 120,style:'color:red'}
                        ]]
                        ,done: function(res, curr, count){

                        }
                    });

                    var outWorkPackList = [];
                    admin.req('enterprisecab/order/admin/inventoryOutWorkRecord/getOutWorkPackList', {jobId:data.id,jobProductId:data.jobProductId}, function (res) {
                        if (res.code == 200) {
                            outWorkPackList = res.data;
                            table.reload('outProductParticularsTable2',{
                                data: outWorkPackList,
                            })
                            console.log(outWorkPackList)
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    }, 'GET');
                    var outProductParticularsTable2 = table.render({
                        elem: '#outProductParticularsTable2',
                        data: outWorkPackList,
                        cellMinWidth: 100,
                        cols: [[
                            {field: 'number', title: '序号', width: 60, type: 'numbers'}
                            , {field: 'id', title: 'ID', width: 60, hide: true}
                            , {field: 'packLabeCode', sort: true, title: '包装编码'}
                            , {field: 'productName', sort: true, title: '物料名称'}
                            , {field: 'packName', title: '包装规格'}
                            //, {field: 'skuCode', title: 'skuCode'}
                            , {field: 'unitName', title: '最小单位'}
                            , {field: 'packLabeQuantity', title: '单位包装数'}
                            , {field: 'quantity', title: '发货数',style:'color:red'}
                            //, {align: 'center', toolbar: '#outProductTableBar', title: '操作', width: 150}//,hide : type == 0 ? true : false
                        ]]
                    });
                    $("#backBtn").click(function () {
                        layer.close(index);//关闭窗口
                    })
                }
            });
        }
        // 导出excel
        $('#inventoryOutPlanBtnExport').click(function () {
            var checkRows = table.checkStatus('inventoryOutPlanTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>